//
//  ViewController.m
//  60003
//
//  Created by student on 16/3/1.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *tableview;
@property (weak, nonatomic) IBOutlet UILabel *textlabel;
@property(strong,nonatomic) NSArray *items;
@property(strong,nonatomic) NSArray *pics;
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self tableviewrefresh];
}
- (IBAction)refresh:(id)sender
{
    NSString *refreshtext = self.items[arc4random()%5][@"chinese"];
    do
    {
        refreshtext = self.items[arc4random()%5][@"chinese"];

    } while ([refreshtext isEqualToString:self.textlabel.text]) ;
    self.textlabel.text = refreshtext;
    [self tableviewrefresh];
}

- (void)tableviewrefresh
{
    for (UIView *view in [self.tableview subviews])
    {
        [view removeFromSuperview];
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:@"codes" ofType:@"plist"];
    NSArray *items = [NSArray arrayWithContentsOfFile:path];
    self.items = items;
    NSLog(@"%@",items[arc4random()%5][@"chinese"]);
    NSMutableArray *pics = [[NSMutableArray alloc]init];
    for (NSDictionary *tmp in items)
    {
        for (int i = 0; i < 6; i++)
        {
            [pics addObject: tmp[@"images"][i]];

        }
    }
    NSLog(@"%@",pics);
    for (int i = 0; i < 8; i++)
    {
        NSMutableArray *deletepic = [[NSMutableArray alloc]init];
        NSString *picname = pics[arc4random()%( pics.count)];
        [deletepic addObject:picname];
        [pics removeObjectsInArray:deletepic];
        NSLog(@"%@ %@",pics,picname);
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(70*(i%4), (i/4)*70, 70, 70)];
        [button setBackgroundImage:[UIImage imageNamed:picname] forState:UIControlStateNormal];
        if ([picname hasPrefix:@"fruit"])
        {
            [button setTitle:@"水果" forState:UIControlStateNormal];
        }
        else if([picname hasPrefix:@"lamp"])
        {
            [button setTitle:@"台灯" forState:UIControlStateNormal];
        }
        else if([picname hasPrefix:@"animal"])
        {
            [button setTitle:@"动物" forState:UIControlStateNormal];
        }
        else if([picname hasPrefix:@"cloth"])
        {
            [button setTitle:@"衣服" forState:UIControlStateNormal];
        }
        else if([picname hasPrefix:@"shoes"])
        {
            [button setTitle:@"鞋子" forState:UIControlStateNormal];
        }
        [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(picclicked:) forControlEvents:UIControlEventTouchUpInside];

        [self.tableview addSubview:button];
    }
}

- (void)picclicked:(UIButton *)sender
{
    if (sender.tag == 0)
    {
    UIView *clicked = [[UIView alloc]initWithFrame:CGRectMake(55, 55, 15, 15)];
    [sender addSubview:clicked];
        clicked.tag = 233;
    clicked.backgroundColor = [UIColor blueColor];
    sender.tag = 666;
    NSLog(@"点按钮");
    }
    else
    {
        for (UIView *view in [sender subviews] )
        {
            if (view.tag == 233)
            {
                
                [view removeFromSuperview];
                sender.tag = 0;
            }
        }
    }
}
- (IBAction)signin:(id)sender
{
    BOOL success = YES;
       NSLog(@"%@",self.textlabel.text);
    for (UIButton *btn in [self.tableview subviews])
    {
         NSLog(@"%ld",(long)btn.tag);
        NSLog(@"%@",btn.titleLabel.text);
        NSLog(@"%@",self.textlabel.text);
        BOOL textandbtn = [btn.titleLabel.text isEqualToString:self.textlabel.text];
        if ((btn.tag != 666) && textandbtn == YES)
        {
            success = NO;
            NSLog(@"第一次验证失败");
            NSLog(@"%ld",(long)btn.tag);
        }
        else if((btn.tag == 666) && textandbtn == NO)
        {
            success = NO;
            NSLog(@"第二次验证失败");
            NSLog(@"%ld",(long)btn.tag);
        }
        
    }
    if (success == YES)
    {
        [self performSegueWithIdentifier:@"newworld" sender:nil];
    }
    else
    {
        UIAlertView *alertview = [[UIAlertView alloc]initWithTitle:@"验证失败" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alertview show];
    }

    
}


@end
